package com.lizard.schedule.net.http.request;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.lizard.schedule.log.ScheduleLog;
import com.lizard.schedule.utils.JsonUtil;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Http请求处理类
 *
 * @param <T>
 */
public class HttpRequest<T> extends Request<T> {

    public static final String ENCODING = "UTF-8";

    public final static String TAG_HTTP_REQUEST = "http_request";

    /**
     * 是否允许打印日志
     */
    private final boolean logEnable = true;

    /**
     * Http回调
     */
    private HttpResponseListener<T> mListener;

    private Class<T> mResponseClazz;

    /**
     * POST内容
     */
    private byte[] mBody;

    private Map<String, String> mHeaders = new HashMap<String, String>();

    public HttpRequest(int method,
                       String url,
                       byte[] body,
                       Map<String, String> headers,
                       Class<T> responseClazz,
                       final HttpResponseListener listener) {
        super(method, url, listener);
        mListener = listener;
        mBody = body;
        if (headers != null) {
            mHeaders.putAll(headers);
        }
        mResponseClazz = responseClazz;
        logHttpRequest(method, url);
    }

    /**
     * 解析server返回的数据
     *
     * @param networkResponse
     * @return
     */
    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) {
        try {
            String jsonString = new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers));
            if (logEnable) {
                log("response json:" + jsonString);
            }
            return Response.success(JsonUtil.getGson().fromJson(jsonString, mResponseClazz),
                    HttpHeaderParser.parseCacheHeaders(networkResponse));
        } catch (UnsupportedEncodingException e) {
            if (logEnable) {
                log(e.getMessage());
            }
            return null;
        }
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return mHeaders;
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        return mBody;
    }

    @Override
    protected String getParamsEncoding() {
        return ENCODING;
    }

    /**
     * 回调解析后的数据
     *
     * @param t
     */
    @Override
    protected void deliverResponse(T t) {
        if (mListener != null) {
            mListener.onResponse(t);
        }
    }

    /**
     * 打印日志
     *
     * @param log
     */
    private void log(String log) {
        if (logEnable) {
            if (log != null) {
                ScheduleLog.v(TAG_HTTP_REQUEST, log);
            }
        }
    }

    /**
     * 打印请求日志
     *
     * @param method
     * @param url
     */
    private void logHttpRequest(int method, String url) {
        // 日志打印
        if (logEnable) {
            StringBuilder sb = new StringBuilder();
            sb.append("url:").append(url).append(" Method:").append(method == Method.GET ? "GET" : "POST");
            log(sb.toString());
        }
    }

}
